package com.wnhz.work.yz.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.wnhz.work.yz.BaseActivity;
import com.wnhz.work.yz.R;
import com.wnhz.work.yz.base.BaseRVAdapter;
import com.wnhz.work.yz.base.BaseViewHolder;
import com.wnhz.work.yz.bean.InstallBean;
import com.wnhz.work.yz.utils.Url;
import com.wnhz.work.yz.utils.xutils3.MyCallBack;
import com.wnhz.work.yz.utils.xutils3.XUtil;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.x;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class InstallActivity extends BaseActivity implements View.OnClickListener {
    private RecyclerView recycler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_install);
        initTitle();
        recycler = (RecyclerView) findViewById(R.id.recycler);
        recycler.setLayoutManager(new GridLayoutManager(this, 2, LinearLayoutManager.VERTICAL, false));
        upLoad();
    }

    private List<InstallBean.InfoEntity> info1 = new ArrayList<>();

    private void upLoad() {
        Map<String, Object> map = new HashMap<>();
        showDialog();
        XUtil.Post(Url.DECORATE_INDEX, map, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                Log.i("----", "onSuccess:= " + result);
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    String res = jsonObject.optString("result");
                    if ("1".equals(res)) {
                        Gson gson = new Gson();
                        InstallBean bean = gson.fromJson(result, InstallBean.class);
                        info1 = bean.getInfo();
                        setData();
                    } else {
                        String info = jsonObject.optString("info");
                        if ("-1".equals(res)) {
                            info = "请重新登录";
                        }
                        MyToast(info);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFinished() {
                closeDialog();
                super.onFinished();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
                closeDialog();
                ex.printStackTrace();
            }
        });
    }

    private void setData() {
        recycler.setAdapter(new BaseRVAdapter(this, info1) {
                                @Override
                                public int getLayoutId(int viewType) {
                                    return R.layout.item_install;
                                }

                                @Override
                                public void onBind(BaseViewHolder holder, final int position) {
                                    if (position % 2 == 1) {
                                        holder.getView(R.id.right).setVisibility(View.GONE);
                                    } else {
                                        holder.getView(R.id.right).setVisibility(View.VISIBLE);
                                    }
                                    holder.setTextView(R.id.name, info1.get(position).getName());
                                    x.image().bind(holder.getImageView(R.id.img), info1.get(position).getLogo_pic());

                                    holder.setTextView(R.id.price, "￥" + info1.get(position).getPrice());

                                    holder.getView(R.id.rl_max).setOnClickListener(
                                            new View.OnClickListener() {
                                                @Override
                                                public void onClick(View v) {
                                                    if ("0".equals(info1.get(position).getType())) {
                                                        switch (Integer.parseInt(info1.get(position).getPid())) {
                                                            case 1:
                                                                startActivity(new Intent(InstallActivity.this, InstallDetails33Activity.class).putExtra("pid", info1.get(position).getPid()).putExtra("name", info1.get(position).getName()).putExtra("type", info1.get(position).getType()));
                                                                break;
                                                            case 2:
                                                            case 3:
                                                                startActivity(new Intent(InstallActivity.this, InstallDetails0Activity.class).putExtra("pid", info1.get(position).getPid()).putExtra("name", info1.get(position).getName()));
                                                                break;
                                                            case 4:
                                                                startActivity(new Intent(InstallActivity.this, InstallDetails1Activity.class).putExtra("pid", info1.get(position).getPid()).putExtra("name", info1.get(position).getName()));
                                                                break;
                                                            case 5:
                                                            case 6:
                                                                startActivity(new Intent(InstallActivity.this, InstallDetails22Activity.class).putExtra("pid", info1.get(position).getPid()).putExtra("name", info1.get(position).getName()).putExtra("type", info1.get(position).getType()));
                                                                break;
                                                        }
                                                    } else if ("1".equals(info1.get(position).getType())) {
                                                        startActivity(new Intent(InstallActivity.this, InstallDetails33Activity.class).putExtra("pid", info1.get(position).getPid()).putExtra("name", info1.get(position).getName()).putExtra("type", info1.get(position).getType()));
                                                    } else if ("2".equals(info1.get(position).getType())) {
                                                        startActivity(new Intent(InstallActivity.this, InstallDetails33Activity.class).putExtra("pid", info1.get(position).getPid()).putExtra("name", info1.get(position).getName()).putExtra("type", info1.get(position).getType()));
                                                    }
                                                }
                                            }
                                    );
                                }
                            }

        );
    }

    private void initTitle() {
        TextView title = (TextView) findViewById(R.id.view_main_middle_title);
        title.setText("安装维修");
        findViewById(R.id.btn_main_left).setOnClickListener(this);
        LinearLayout ll_search = (LinearLayout) findViewById(R.id.ll_search);
        ll_search.setOnClickListener(this);
        ll_search.setVisibility(View.GONE);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_main_left:
                finish();
                break;
            case R.id.ll_search:
                startActivity(new Intent(InstallActivity.this, InstallSearchActivity.class));
                break;
        }
    }
}
